package com.dirox.jira.notes.issuetab;

import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.jql.builder.JqlQueryBuilder;
import com.atlassian.jira.plugin.issuetabpanel.AbstractIssueAction;
import com.atlassian.jira.plugin.issuetabpanel.IssueTabPanelModuleDescriptor;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.security.PermissionManager;
import com.atlassian.jira.security.Permissions;
import com.atlassian.jira.security.groups.GroupManager;
import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.query.Query;
import com.dirox.jira.notes.entities.Note;
import com.dirox.jira.notes.service.NoteService;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;


public class NoteIssueAction extends AbstractIssueAction {
	private final JiraAuthenticationContext context;
	private SearchService searchService;
	private NoteService noteService;
	private Issue currentIssue;
	private UserUtil userUtil;
	private GroupManager groupManager;
	private PermissionManager permissionManager;

	public NoteIssueAction(IssueTabPanelModuleDescriptor descriptor,
	                       JiraAuthenticationContext context,
	                       SearchService searchService,
	                       NoteService noteService, UserUtil userUtil,
	                       GroupManager groupManager,
	                       PermissionManager permissionManager) {
		super(descriptor);
		this.context = context;
		this.noteService = checkNotNull(noteService);
		this.searchService = checkNotNull(searchService);
		this.userUtil = checkNotNull(userUtil);
		this.groupManager = checkNotNull(groupManager);
		this.permissionManager = checkNotNull(permissionManager);
	}

	public List<Issue> getIssues() {
		Query query = JqlQueryBuilder.newClauseBuilder()
		                             .buildQuery();

		try {
			List<Issue> issues = searchService.search(
				context.getLoggedInUser(),
				query,
				PagerFilter.getUnlimitedFilter())
			                                  .getIssues();

			for (Issue issue : issues) {
				if (!permissionManager.hasPermission(
						Permissions.BROWSE,
						issue,
						context.getLoggedInUser())) {
					issues.remove(issue);
				}
			}

			return issues;
		} catch (SearchException e) {
			e.printStackTrace();

			return null;
		}
	}

	@Override
	public Date getTimePerformed() {
		return new Date();
	}

	@SuppressWarnings({"unchecked", "rawtypes"})
	@Override
	protected void populateVelocityParams(Map params) {
		com.dirox.jira.notes.entities.User owner = noteService.currentUser();
		List<Note> notes = noteService.getNotes(currentIssue.getKey(),
		                                        owner);
		List<Issue> issues = getIssues();

		SortedSet<User> users = userUtil.getAllUsersInGroups(
			groupManager.getAllGroups());
		List<Note> memos = noteService.getMemos(currentIssue.getKey(),
		                                        owner);

		if ((notes.size() > 0) || (memos.size() > 0)) {
			params.put("hasLastNote", 1);
		} else {
			params.put("hasLastNote", 0);
		}

		params.put("memos", memos);
		params.put("issues", issues);
		params.put("notes", notes);
		params.put("users", users);
		params.put("currentIssue", this.currentIssue);
		params.put("currentUser",
		           context.getLoggedInUser());
	}

	public Issue getIssue() {
		return this.currentIssue;
	}

	public void setIssue(Issue i) {
		this.currentIssue = i;
	}
}
